package com.xrigau.gametest.menu;

import java.util.ArrayList;

import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.scene.menu.animator.SlideMenuAnimator;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.modifier.ease.IEaseFunction;

public class TopMenuAnimator extends SlideMenuAnimator {
	
	protected static final float DURATION = 0.5f;
	
	// ===========================================================
		// Constants
		// ===========================================================

		// ===========================================================
		// Fields
		// ===========================================================

		// ===========================================================
		// Constructors
		// ===========================================================

		public TopMenuAnimator(){
			super();
		}

		public TopMenuAnimator(final IEaseFunction pEaseFunction) {
			super(pEaseFunction);
		}

		public TopMenuAnimator(final HorizontalAlign pHorizontalAlign) {
			super(pHorizontalAlign);
		}

		public TopMenuAnimator(final HorizontalAlign pHorizontalAlign, final IEaseFunction pEaseFunction) {
			super(pHorizontalAlign, pEaseFunction);
		}

		public TopMenuAnimator(final float pMenuItemSpacing) {
			super(pMenuItemSpacing);
		}

		public TopMenuAnimator(final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
			super(pMenuItemSpacing, pEaseFunction);
		}

		public TopMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing) {
			super(pHorizontalAlign, pMenuItemSpacing);
		}

		public TopMenuAnimator(final HorizontalAlign pHorizontalAlign, final float pMenuItemSpacing, final IEaseFunction pEaseFunction) {
			super(pHorizontalAlign, pMenuItemSpacing, pEaseFunction);
		}

		// ===========================================================
		// Getter & Setter
		// ===========================================================

		// ===========================================================
		// Getter & Setter
		// ===========================================================

		protected float getMaximumHeight(final ArrayList<IMenuItem> pMenuItems) {
			float maximumHeight = Float.MIN_VALUE;
			for(int i = pMenuItems.size() - 1; i >= 0; i--) {
				final IMenuItem menuItem = pMenuItems.get(i);
				maximumHeight = Math.max(maximumHeight, menuItem.getHeightScaled());
			}
			
			return maximumHeight;
		}
		
		// ===========================================================
		// Methods for/from SuperClass/Interfaces
		// ===========================================================

		@Override
		public void buildAnimations(final ArrayList<IMenuItem> pMenuItems, final float pCameraWidth, final float pCameraHeight) {
			final IEaseFunction easeFunction = this.mEaseFunction;
			final float maximumHeight = this.getMaximumHeight(pMenuItems);
			float offsetX = 0;
			
			final int menuItemCount = pMenuItems.size();
			for(int i = 0; i < menuItemCount; i++) {
				final IMenuItem menuItem = pMenuItems.get(i);

				final MoveModifier moveModifier = new MoveModifier(DURATION, offsetX, offsetX, -maximumHeight, 0, easeFunction);
				moveModifier.setAutoUnregisterWhenFinished(false);
				menuItem.registerEntityModifier(moveModifier);

				offsetX += menuItem.getWidth() + this.mMenuItemSpacing;
			}
		}

		@Override
		public void prepareAnimations(final ArrayList<IMenuItem> pMenuItems, final float pCameraWidth, final float pCameraHeight) {
			final float maximumHeight = this.getMaximumHeight(pMenuItems);
			final float baseY = (pCameraHeight - maximumHeight) * 0.5f;
			final float menuItemSpacing = this.mMenuItemSpacing;

			float offsetX = 0;
			final int menuItemCount = pMenuItems.size();
			for(int i = 0; i < menuItemCount; i++) {
				final IMenuItem menuItem = pMenuItems.get(i);

				menuItem.setPosition(offsetX, baseY);

				offsetX += menuItem.getWidth() + menuItemSpacing;
			}
		}

		// ===========================================================
		// Methods
		// ===========================================================

		// ===========================================================
		// Inner and Anonymous Classes
		// ===========================================================

}